import { DictValueEnum } from "@/components/DictTag";
import { http } from "@/utils/http/axios";

export function getDict(type: string, isDigital: boolean = false): Promise<DictValueEnum> {
	return new Promise(resolve => {
		return http
			.request<API.Response.BasicResponse>(
				{
					url: `/system/dict/data/type/${type}`,
					method: "GET"
				},
				{
					isTransformResponse: true
				}
			)
			.then((data: any) => {
				const opts: DictValueEnum = {};
				data.forEach((item: any) => {
					opts[item.dictValue] = {
						text: item.dictLabel,
						label: item.dictLabel,
						value: isDigital ? Number(item.dictValue) : item.dictValue,
						key: item.id,
						color: item.color
					};
				});
				return resolve(opts);
			})
			.catch(() => {
				return resolve({});
			});
	});
}
